package com.yumeng.example.test.controller;

import com.yumeng.common.api.ApiResult;
import com.yumeng.common.api.PageData;
import com.yumeng.common.buss.area.AreaHelper;
import com.yumeng.common.utils.JacksonUtils;
import com.yumeng.example.test.dto.page.TestPageDto;
import com.yumeng.example.test.entity.Test;
import com.yumeng.example.test.service.ExampleTestService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author wxd
 * @date 2021/8/17 17:20
 */
@Slf4j
@RestController
//@ApiSupport(order = 2)
@Tag(name = "测试Controller", description = "测试接口集合")
@RequestMapping("/example/test")
public class ExampleTestController {

    private final String prefix = "demo/test";
    private final String permPrefix = "demo:test:";

    @Autowired
    private ExampleTestService testService;

    /**
     * 测试DemoDoTest
     *
     * @return
     */
    @Operation(summary = "doTest()")
    @GetMapping("/doTest.do")
    public String doTest() {
        //testService.doTest();
        return String.format("当前系统地区级别：%s，编码：%s，名称：%s",
                AreaHelper.getCurrSysAreaLevel().getDesc(),
                AreaHelper.getCurrSysAreaCode(),
                AreaHelper.getCurrSysAreaName());
    }

    @Operation(summary = "testExecute()")
    @GetMapping("/testExecute.do")
    public ApiResult testExecute(Integer type) {
        boolean b = testService.testExecute(type);
        return ApiResult.resultFlag(b);
    }

    @Operation(summary = "testQuery()")
    @GetMapping("/testQuery.do")
    public ApiResult testQuery(Integer type) {
        Object obj = testService.testQuery(type);
        return ApiResult.successData(obj);
    }

    @Operation(summary = "pageTest()")
    @GetMapping("/pageTest.do")
    public PageData<Test> getTestPageList(@Validated TestPageDto dto) {
        PageData<Test> page = testService.getTestPageList(dto, dto.getType());
        return page;
    }

    @Operation(summary = "GetOne")
    @GetMapping("/getOneTest.do")
    public Test getOneTest(Long testId) {
        Test test = testService.getOneTest(testId);
        return test;
    }

    @Operation(summary = "GetOneWithEntity")
    @GetMapping("/getOneTestWithEntity.do")
    public ResponseEntity<ApiResult> getOneTestWithEntity(Long testId) {
        Test test = testService.getOneTest(testId);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ApiResult.successData(test));
    }

    @Operation(summary = "GetOneOnlyEntity")
    @GetMapping("/getOneTestOnlyEntity.do")
    public ResponseEntity<Test> getOneTestOnlyEntity( Long testId) {
        Test test = testService.getOneTest(testId);
        return ResponseEntity.ok(test);
    }

    @Operation(summary = "注入-已测：insertBatch")
    @GetMapping("/insertBatch.do")
    public boolean insertBatch() {
        return testService.insertBatch();
    }

    @Operation(summary = "注入-已测：updateBatchIds")
    @GetMapping("/updateBatchIds.do")
    public boolean updateBatchIds() {
        return testService.updateBatchIds();
    }

    @Operation(summary = "注入-已测：logicDeleteById")
    @GetMapping("/logicDeleteById.do")
    public boolean logicDeleteById(@RequestParam Long id) {
        return testService.logicDeleteById(id);
    }

    @Operation(summary = "注入-已测：logicDeleteBatchIds")
    @GetMapping("/logicDeleteBatchIds.do")
    public boolean logicDeleteBatchIds(@RequestParam List<Long> ids) {
        return testService.logicDeleteBatchIds(ids);
    }

    @Operation(summary = "注入-已测：logicDelete")
    @GetMapping("/logicDelete.do")
    public boolean logicDelete() {
        return testService.logicDelete();
    }

    @Operation(summary = "注入-已测：logicDeleteByMap")
    @GetMapping("/logicDeleteByMap.do")
    public boolean logicDeleteByMap() {
        return testService.logicDeleteByMap();
    }

    @Operation(summary = "测试乐观锁：testVersion")
    @GetMapping("/testVersion.do")
    public boolean testVersion(@RequestParam Integer type){
        return testService.testVersion(type);
    }

    @GetMapping("/testJacksonObjectMapper.do")
    public ApiResult testJacksonObjectMapper(){
        Test test = testService.getOneTest(1433497113420890112L);
        String json = JacksonUtils.toJsonString(test);
        log.info("Json测试：{}", json);
        return ApiResult.successData(test);
    }
}
